package com.ln.adoms.dao;

import com.ln.adoms.entity.DepartSetting;
import com.ln.adoms.entity.Order;
import com.ln.adoms.entity.neum.Status;
import com.ln.core.orm.hibernate.BaseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public class OrderDao extends BaseDao<Order> {
    @Autowired
    private DepartSettingDao departSettingDao;

    @Override
    public void update(Order entity) {
        //如果订单已经发货就设置自动收货时间，如系统有配置，则设置订单的自动收货时间
        if (Status.ORDER_HAS_SHIPED.equals(entity.getStatus())) {
            DepartSetting setting = departSettingDao.getAutoReceiveOrderSetting(entity.getDepartment().getId());
            if (setting != null) {
                Date autoReceiveTime = new Date();
                autoReceiveTime.setTime(autoReceiveTime.getTime() + Integer.parseInt(setting.getConfig()) * 24 * 60 * 60 * 1000l);
                entity.setAutoReceiveTime(autoReceiveTime);
            }
        }
        super.update(entity);
    }
}
